package com.lsege.controller.backstage;

import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.Indiana;
import com.lsege.entity.backstage.IndianaOrder;
import com.lsege.entity.backstage.Prize;
import com.lsege.service.backstage.PrizeService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.stream.MemoryCacheImageInputStream;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2017/7/8.
 */
@RestController
@RequestMapping("/prize")
public class PrizeController {
    @Autowired
    PrizeService prizeService;
    /**
     * 添加奖品
     * */
    @PostMapping("addPrize")
    public JsonResult addPrize(Prize prize){
        JsonResult json = new JsonResult();
            try {
                prizeService.addPrizeInfo(prize);
                json.setSuccess(true);
                json.setMessage("添加成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("添加失败");
            }
        return json;
    }
    /**
     * 获取奖品列表
     * */
    @GetMapping("getPrizeList")
    public JsonResult getPrizeList(Integer pageNum, Integer pageSize){
        JsonResult json = new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Prize> list = prizeService.queryPrizeList();
            Long total = prizeService.queryPrizeCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取列表失败");
        }
        return json;
    }

    /**
     * 获取夺宝列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getIndianaList")
    public JsonResult getIndianaList(Integer pageNum,Integer pageSize){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Prize> list=prizeService.getIndianaList();
            Long total=prizeService.getIndianaCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取列表失败");
        }
        return json;
    }

    /**
     * 获取夺宝订单记录
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getIndianaOrder")
    public JsonResult getIndianaOrder(Integer pageNum,Integer pageSize){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<IndianaOrder> list=prizeService.getIndianaOrder();
            Long total=prizeService.getIndianaOrderCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取列表失败");
        }
        return json;
    }

    /**
     *奖品下架
     * @param prizeCode
     * @return
     */
    @PostMapping("deletePrize")
    public JsonResult deletePrize(String prizeCode){
        JsonResult json=new JsonResult();
        Integer num=prizeService.queryPrizeByCode(prizeCode);
        if (num == 0){
            json.setMessage("该奖品不存在");
            json.setSuccess(false);
        }else {
            try {
                prizeService.deletePrize(prizeCode);
                json.setSuccess(true);
                json.setMessage("下架成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("下架失败");
            }
        }
        return json;
    }

    /**
     * 开启夺宝
     * @param indiana
     * @return
     */
    @PostMapping("createIndianaOrder")
    public JsonResult createIndianaOrder(Indiana indiana){
        JsonResult json=new JsonResult();
        try {
            prizeService.addIndiana(indiana);
            json.setSuccess(true);
            json.setMessage("夺宝开启成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("夺宝开启失败");
        }
        return json;
    }

    /**
     * 后台开启夺宝
     * @param indiana
     * @return
     */
    @PostMapping("startIndiana")
    public JsonResult startIndiana(Indiana indiana){
        JsonResult json=new JsonResult();
        try {
            prizeService.addIndiana(indiana);
            prizeService.openPrizeTimes(indiana);
            json.setSuccess(true);
            json.setMessage("夺宝开启成功");
        }catch (Exception e){
            json.setSuccess(false);
            json.setMessage("夺宝开启失败");
        }
        return json;
    }

    /**
     * 修改快递
     * @param indianaOrder
     * @return
     */
    @PostMapping("changeExpress")
    public JsonResult changeExpress(IndianaOrder indianaOrder){
        JsonResult json=new JsonResult();
        String ti=indianaOrder.getSendState();
        if (indianaOrder.getSendState().equals("2")){
            Date date=new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            indianaOrder.setSendDate(sf.format(date));
        }
        try {

            prizeService.changeExpress(indianaOrder);
            json.setSuccess(true);
            json.setMessage("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("修改失败");
        }
        return json;
    }

    @GetMapping("getIndianaOrderByCode")
    public JsonResult getIndianaOrderByCode(String orderCode){
        JsonResult json=new JsonResult();
        Long num=prizeService.getIndianaOrderByCodeCount(orderCode);
        if (num!=0){
            try {
                List<IndianaOrder> list=prizeService.getIndianaOrderByCode(orderCode);
                json.setData(list);
                json.setSuccess(true);
                json.setMessage("查询成功");
            }catch (Exception e){
                e.printStackTrace();
                json.setSuccess(false);
                json.setMessage("查询失败");
            }
        }else {
            json.setSuccess(false);
            json.setMessage("未找到该订单");
        }
        return json;
    }

//    /**
//     * 删除奖品
//     * */
//    @GetMapping("deletePrize")
//    public JsonResult deletePrize(Prize prize){
//        JsonResult json = new JsonResult();
//        Long num = prizeService.queryPrizeById(prize.getId());
//        if(num!=0){
//            Long isStart = prizeService.queryPrizeStatus(prize);
//            if(isStart == 2){
//                try {
//                    prizeService.deletePrize(prize.getId());
//                    json.setSuccess(true);
//                    json.setMessage("下架成功");
//                } catch (Exception e){
//                    e.printStackTrace();
//                    json.setSuccess(false);
//                    json.setMessage("下架失败");
//                }
//            }
//        } else {
//            json.setSuccess(false);
//            json.setMessage("该奖品不存在");
//        }
//        return json;
//    }
}
